package com.gr.staffpm.tasks.service;

import java.util.Iterator;

import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.model.IModel;

import com.gr.staffpm.datatypes.Task;
import com.gr.staffpm.datatypes.TaskComment;
import com.gr.staffpm.tasks.model.DetachableCommentModel;


public class SortableCommentDataProvider extends SortableDataProvider<TaskComment> {

    private final TaskService taskService;
    private final Task task;
    private static final long serialVersionUID = 1L;

    public SortableCommentDataProvider(TaskService taskService, Task task) {
        setSort(TaskComment.CREATED, false);
        this.taskService = taskService;
        this.task = task;
    }

    @Override
    public Iterator<? extends TaskComment> iterator(int first, int count) {
        SortParam sp = getSort();
        return taskService.getComments(task, first, count, sp.getProperty(), sp.isAscending()).iterator();
    }

    @Override
    public int size() {
        return taskService.getAllComments(task).size();
    }

    @Override
    public IModel<TaskComment> model(TaskComment comment) {
        return new DetachableCommentModel(taskService, comment);
    }

}
